from Products.KnowledgeEngine.KERL.Tokens.Token import Token
from Products.KnowledgeEngine.Services.MySQLFiveAssessmentStorageService.MySQLFiveAssessmentStorageService import qstr

class PairToken(Token):
    
    _implements = Token._implements + ('PairToken',)
    
    def __init__(self, object, namespace):
        if not (type(object) is tuple and len(object) == 2):
            raise "Creating PairToken with non-pair value %s" % `object`
        
        if not (type( object[0] ) in (float, int)) and (type(object[1]) in (float, int)):
            raise "Creating PairToken with non-numeric values"
        
        Token.__init__( self, object, namespace )
    
    def toEnglish(self):
        """ Compiles this token into english """
        
        return str(self.object[0]) + " and " + str(self.object[1])

    def toSQL(self, quote="'"):
        """ Compiles this token into SQL """
        
        return "%s AND %s" % ( qstr( self.object[0] ), qstr( self.object[1] ))
        
        #return qstr(self.object, quote=quote)

    def toJavascript(self): # TODO
        
        return qstr(self.getObject())

    def toKERL(self):
        return "%s,%s" % (self.object[0], self.object[1])
        #return qstr(self.getObject())


    def getRangeMin(self):
        return self.getObject()[0]
    
    def getRangeMax(self):
        return self.getObject()[1]  

    def __str__(self):
        return self.getObject()